<template>
  <el-dialog
     v-drag
    :title="!dataForm.stockId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :inline="true" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="库存id" prop="stockId">
      <el-input size="mini"  v-model="dataForm.stockId" placeholder="库存id" clearable></el-input>
    </el-form-item>
                                                                                                                                                                                                                                                                </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          stockId: 0,
          productId: '',
          productCode: '',
          productName: '',
          wareId: '',
          categoryId: '',
          categoryName: '',
          genericName: '',
          producingArea: '',
          supplierId: '',
          supplierName: '',
          supplierMobile: '',
          standard: '',
          unit: '',
          mnemonicName: '',
          batchNo: '',
          lotNo: '',
          storageNo: '',
          acceptNo: '',
          receiveNo: '',
          orderNo: '',
          supplierDeliveryNo: '',
          purchaseInvoiceNo: '',
          purchaseinvoiceCode: '',
          stockNum: '',
          purchaseNum: '',
          returnNum: '',
          splitNum: '',
          saleNum: '',
          checkInNum: '',
          checkOutNum: '',
          destroyNum: '',
          purchasePrice: '',
          retailPrice: '',
          salePrice: '',
          memberPrice: '',
          barCode: '',
          isRxDrug: '',
          approvalNo: '',
          produceFactory: '',
          dosageForm: '',
          expiryDate: '',
          isMedicare: '',
          isChinaDrug: '',
          isGmp: '',
          effect: '',
          brandId: '',
          brandName: '',
          isCommission: '',
          commissionRatio: '',
          remark: '',
        },
        dataRule: {
          productId: [
            { required: true, message: '产品Id不能为空', trigger: 'blur' }
          ],
          productCode: [
            { required: true, message: '产品编号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          wareId: [
            { required: true, message: '仓库id不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '类别id不能为空', trigger: 'blur' }
          ],
          categoryName: [
            { required: true, message: '类别名称不能为空', trigger: 'blur' }
          ],
          genericName: [
            { required: true, message: '通用名不能为空', trigger: 'blur' }
          ],
          producingArea: [
            { required: true, message: '产品区域不能为空', trigger: 'blur' }
          ],
          supplierId: [
            { required: true, message: '供应商id不能为空', trigger: 'blur' }
          ],
          supplierName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          supplierMobile: [
            { required: true, message: '供应商联系方式不能为空', trigger: 'blur' }
          ],
          standard: [
            { required: true, message: '规格不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          mnemonicName: [
            { required: true, message: '助记名不能为空', trigger: 'blur' }
          ],
          batchNo: [
            { required: true, message: '批号不能为空', trigger: 'blur' }
          ],
          lotNo: [
            { required: true, message: '采购批次号不能为空', trigger: 'blur' }
          ],
          storageNo: [
            { required: true, message: '入库号不能为空', trigger: 'blur' }
          ],
          acceptNo: [
            { required: true, message: '验收号不能为空', trigger: 'blur' }
          ],
          receiveNo: [
            { required: true, message: '收货单号不能为空', trigger: 'blur' }
          ],
          orderNo: [
            { required: true, message: '采购订单号不能为空', trigger: 'blur' }
          ],
          supplierDeliveryNo: [
            { required: true, message: '供应商配送单号不能为空', trigger: 'blur' }
          ],
          purchaseInvoiceNo: [
            { required: true, message: '采购发票号不能为空', trigger: 'blur' }
          ],
          purchaseinvoiceCode: [
            { required: true, message: '采购发票编码不能为空', trigger: 'blur' }
          ],
          stockNum: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          purchaseNum: [
            { required: true, message: '采购数量不能为空', trigger: 'blur' }
          ],
          returnNum: [
            { required: true, message: '采购退货数量不能为空', trigger: 'blur' }
          ],
          splitNum: [
            { required: true, message: '拆零数量不能为空', trigger: 'blur' }
          ],
          saleNum: [
            { required: true, message: '销售数量不能为空', trigger: 'blur' }
          ],
          checkInNum: [
            { required: true, message: '盘盈数量不能为空', trigger: 'blur' }
          ],
          checkOutNum: [
            { required: true, message: '盘亏数量不能为空', trigger: 'blur' }
          ],
          destroyNum: [
            { required: true, message: '销毁数量不能为空', trigger: 'blur' }
          ],
          purchasePrice: [
            { required: true, message: '采购价不能为空', trigger: 'blur' }
          ],
          retailPrice: [
            { required: true, message: '指导零售价不能为空', trigger: 'blur' }
          ],
          salePrice: [
            { required: true, message: '售价不能为空', trigger: 'blur' }
          ],
          memberPrice: [
            { required: true, message: '会员价不能为空', trigger: 'blur' }
          ],
          barCode: [
            { required: true, message: '条码不能为空', trigger: 'blur' }
          ],
          isRxDrug: [
            { required: true, message: '是否处方药1是不能为空', trigger: 'blur' }
          ],
          approvalNo: [
            { required: true, message: '批准文号不能为空', trigger: 'blur' }
          ],
          produceFactory: [
            { required: true, message: '厂家不能为空', trigger: 'blur' }
          ],
          dosageForm: [
            { required: true, message: '剂型不能为空', trigger: 'blur' }
          ],
          expiryDate: [
            { required: true, message: '有效期不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.stockId = id || 0
        this.visible = true
        this.$nextTick(async () => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.stockId) {
            const res = await this.$http({
              url: `/product/stock/info/${this.dataForm.stockId}`,
              method: 'get',
              params: this.$http.adornParams()
            })
                let data = res.data
                if (data && data.code === 0) {
                    this.dataForm = data.productStock
                }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url:`/product/stock/${!this.dataForm.stockId ? 'save' : 'update'}`,
              method: 'post',
             data: this.$http.adornData( this.dataForm)
            })
            let data = res.data
            if (data && data.code === 0) {
                this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                }
            })
            } else {
                this.$message.error(data.msg)
            }
          }
        })
      }
    }
  }
</script>
